<?php
declare (strict_types=1);

namespace app\api\controller;

use addons\Pay\driver\WeChat;
use addons\V2\admin\model\Product;
use addons\V2\admin\model\ProductCate;
use addons\WeChatMini\facade\WeChatMini;
use app\common\model\Carousel;
use app\common\model\News;
use app\common\model\User as UserModel;
use app\common\tool\Result;

class Index extends Base
{

    public function get_openid()
    {
        $post = $this->request->post();
        $result = WeChatMini::session($post['code']);
        if($result['openid'])
        {
            UserModel::update(['openid'=>$result['openid']],['id'=>$this->user->id]);
            return Result::success('获取数据成功！');
        }else{
            return Result::fail('获取数据成功！');
        }
    }

    /**
     * get: 轮播图
     * path: carousel
     * method: carousel
     */
    public function carousel(): \think\Response
    {
        $data['carousel'] = Carousel::getList(['delete_time'=>0]);
        return Result::success('获取成功', $data);
    }


    /**
     * get:  新闻轮播
     * path: news_reveal
     * method: news_reveal
     */
    public function news_reveal(): \think\Response
    {
        $news = News::where(['delete_time'=>0,'status'=>1,'type'=>1])->order('sort','asc')->order('create_time','desc')->field('id,title')->select();
        return Result::success('获取成功', $news);
    }

    /**
     * get: 商品分类
     * path: get_cate
     * method: get_cate
     */
    public function get_cate(): \think\Response
    {
        $cate = ProductCate::where(['status' => 1, 'delete_time' => 0])->order('sort','asc')->field('id,icon,title')->select();
        if (isset($cate)) {
            return Result::success('获取成功', $cate);
        } else {
            return Result::fail('获取失败');
        }
    }

    /**
     * get: 爆款商品
     * path: hot_product
     * method: hot_product
     */
    public function hot_product(): \think\Response
    {
        $recommend = Product::where(['delete_time'=>0,'status'=>1,'hot'=>1])->order('sort','asc')->order('create_time','desc')->field('id,title,price,vip_price,cash,icon,integral,pay_method')->select();
        foreach ($recommend as $k=>$v)
        {
            if($v->pay_method==1)
            {
                $v->vip_price = floor((int)$v->vip_price);
            }

        }
        return Result::success('获取成功', $recommend);

    }

    /**
     * get: 推荐商品
     * path: recommend
     * method: recommend
     */
    public function recommend(): \think\Response
    {
        $recommend = Product::where(['delete_time'=>0,'status'=>1,'pick'=>1])->order('sort','asc')->order('create_time','desc')->field('id,title,price,vip_price,cash,icon,integral,pay_method')->select();
        foreach ($recommend as $k=>$v)
        {
            if($v->pay_method==1)
            {
                $v->vip_price = floor((int)$v->vip_price);
            }

        }
        return Result::success('获取成功', $recommend);

    }




}
